var prefix = api + "/admin/sys-file";

$(function () {
  var downFlag = permissions.sys_file_down
  var deleteFlag = permissions.sys_file_del
  var options = {
    url: prefix + "/page",
    removeUrl: prefix + "/{id}",
    sortName: "createTime",
    sortOrder: "desc",
    modalName: "文件",
    escape: true,
    columns: [{
      title: "序号",
      formatter: function (value, row, index) {
        return $.table.serialNumber(index);
      }
    },
      {
        field: 'bucketName',
        title: '空间'
      },

      {
        field: 'original',
        title: '原文件名'
      },
      {
        field: 'fileName',
        title: '存储文件名'
      },
      {
        field: 'type',
        title: '文件类型'
      },
      {
        field: 'fileSize',
        title: '文件大小(byte)'
      },
      {
        field: 'createUser',
        title: '上传人'
      },
      {
        field: 'createTime',
        sortable: true,
        title: '创建时间'
      },
      {
        title: '操作',
        align: 'center',
        formatter: function (value, row, index) {
          var actions = [];
          if (deleteFlag) {
            actions.push('<a class="btn btn-danger btn-xs " href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a> ');
          }
          if (downFlag) {
            actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="$.common.fileNameDoDownload(\'' + row.fileName + '\')"><i class="fa fa-download"></i>下载</a>');
          }
          if (!isIE89){
            actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="viewFile(\'' + row.fileName + '\',\'' + row.bucketName + '\')"><i class="fa fa-eye"></i>预览</a>');
          }
          return actions.join('');
        }
      }]
  };
  $.table.init(options);
});

function uploadFile() {
  var url = "/pages/sysadm/file/upload";
  $.modal.open("上传文件", url, '380', '300');
}

function refreshTable() {
  $.table.refresh();
}

function viewFile(filename,bucketName) {
  var urlView = api + '/admin/sys-file/fileView/' + bucketName + '/' + filename
  var windowUrl = window.URL || window.webkitURL;//处理浏览器兼容性
  var xhr = new XMLHttpRequest();
  xhr.open("GET", urlView, true);
  xhr.responseType = "blob";
  // xhr.setRequestHeader("Authorization", 'Bearer ' + session.get('token'),);
  $.modal.loading("正在获取预览文件。。。");
  xhr.onload = function () {
    $.modal.closeLoading();
    if (this.status == 200) {
      var blob = this.response;
      var url="";
      if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(blob);
      } else if (window.webkitURL != undefined) { // webkit or chrome
        try {
          url = window.webkitURL.createObjectURL(blob);
        } catch (error) {}
      } else if (window.URL != undefined) { // mozilla(firefox)
        try {
          url = window.URL.createObjectURL(blob);
        } catch (error) {}
      }
      // window.open('/static/js/pdf/web/viewer.html?file='+encodeURIComponent(url))
    }
  }
  xhr.send();



}

/*
function submitHandler(index, layero) {
  var iframe = layero.find("iframe")[0].contentWindow
  $.ajax({
    url: api + "/admin/sys-file/upload",
    data: iframe.formdata,
    type: "post",
    processData: false,
    contentType: false,
    beforeSend: function () {
      $.modal.loading("上传中...");
    },
    success: function (result) {
      $.modal.closeLoading();
      $.modal.msgSuccess(result.msg == null ? "上传成功" : result.msg);
      layer.close(index);
      $.table.refresh();
    }
  })
}
*/
